﻿Imports System.Drawing
Imports UCBase

Public Class TrackerTester
    Implements UCBase.I_ImageExchange
    Private WithEvents InX As UCBase.I_IntegerExchange
    Private WithEvents InY As UCBase.I_IntegerExchange
    Private Bmp As Bitmap
    Private c_X As Integer = 75
    Private c_Y As Integer = 75

    Private Sub DrawBall()
        Dim Ball As New Bitmap(50, 50, Imaging.PixelFormat.Format24bppRgb)
        Dim G As Graphics = Graphics.FromImage(Ball)
        G.Clear(Color.Black)
        G.FillEllipse(Brushes.White, 0, 0, 50, 50)
        G.Dispose()
        Bmp = New Bitmap(200, 200, Imaging.PixelFormat.Format24bppRgb)
        Dim Gp As Graphics = Graphics.FromImage(Bmp)
        Gp.Clear(Color.Black)
        Gp.DrawImage(Ball, c_X, c_Y)
        PictureBox1.Image = Bmp
        RaiseEvent ImageOut1(Bmp)
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Enabled = False
        Dim nX As Integer
        Dim nY As Integer
        Dim Xd As Integer
        Dim Yd As Integer

        nX = CInt(Int(((KnobControl1.Value + 1) * Rnd()) + 0))
        nY = CInt(Int(((KnobControl1.Value + 1) * Rnd()) + 0))
        Xd = CInt(Int((2 * Rnd()) + 0))
        Yd = CInt(Int((2 * Rnd()) + 0))

        Debug.Print(nX & "-" & nY & "-" & Xd & "-" & Yd)

        If Xd = 1 Then
            c_X = c_X + nX
        Else
            c_X = c_X - nX
        End If
        If c_X < 0 Then c_X = 0
        If c_X > 150 Then c_X = 150

        If Yd = 1 Then
            c_Y = c_Y + nY
        Else
            c_Y = c_Y - nY
        End If
        If c_Y < 0 Then c_Y = 0
        If c_Y > 150 Then c_Y = 150

        DrawBall()

    End Sub

    Private Sub Button3D1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3D1.Click
        Timer1.Enabled = True
    End Sub

    Private Sub TrackerTester_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        '445, 395
        Me.Width = 445
        Me.Height = 395

    End Sub

    Private Sub Button3D2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3D2.Click
        c_X = 75
        c_Y = 75
        Timer1.Enabled = False
        DrawBall()
    End Sub
    Public Event ImageOut1(ByVal Image As System.Drawing.Bitmap) Implements UCBase.I_ImageExchange.ImageOut1
    Private Sub Socket2_Connect() Handles Socket2.Connect
        Dim S As Socket = Socket2.Remote
        InX = S.Parent
    End Sub

    Private Sub Socket3_Connect() Handles Socket3.Connect
        Dim S As Socket = Socket3.Remote
        InY = S.Parent
    End Sub

    Private Sub InX_IntegerRx2(ByVal IntA As Integer, ByVal IntB As Integer) Handles InX.IntegerRx2
        If IntB = 1 Then
            c_X = c_X + IntA
        Else
            c_X = c_X - IntA
        End If
        If c_X < 0 Then c_X = 0
        If c_X > 150 Then c_X = 150
        Timer1.Enabled = False
        DrawBall()
    End Sub

    Private Sub InY_IntegerRx2(ByVal IntA As Integer, ByVal IntB As Integer) Handles InY.IntegerRx2
        If IntB = 1 Then
            c_Y = c_Y + IntA
        Else
            c_Y = c_Y - IntA
        End If
        If c_Y < 0 Then c_Y = 0
        If c_Y > 150 Then c_Y = 150
        Timer1.Enabled = False
        DrawBall()
    End Sub
End Class
